const fs = require('fs');
const path = require('path');

// 需要更新的Entity文件列表
const entityFiles = [
  'nestjs-app/src/entities/user.entity.ts',
  'nestjs-app/src/entities/character.entity.ts',
  'nestjs-app/src/entities/character-location.entity.ts',
  'nestjs-app/src/entities/chat-message.entity.ts',
  'nestjs-app/src/entities/character-inventory.entity.ts',
  'nestjs-app/src/entities/private-message.entity.ts',
  'nestjs-app/src/entities/system-message.entity.ts',
  'nestjs-app/src/entities/character-resources.entity.ts',
  'nestjs-app/src/entities/character-gem-inventory.entity.ts',
  'nestjs-app/src/entities/social-relation.entity.ts',
  'nestjs-app/src/entities/shop-goods.entity.ts',
  'nestjs-app/src/entities/player-online-time.entity.ts',
  'nestjs-app/src/entities/mount-training.entity.ts',
  'nestjs-app/src/entities/mount-base.entity.ts',
  'nestjs-app/src/entities/monthly-card.entity.ts',
  'nestjs-app/src/entities/map-area.entity.ts',
  'nestjs-app/src/entities/item.entity.ts',
  'nestjs-app/src/entities/gem-base.entity.ts',
  'nestjs-app/src/entities/gem-attribute.entity.ts',
  'nestjs-app/src/entities/faction.entity.ts',
  'nestjs-app/src/entities/faction-member.entity.ts',
  'nestjs-app/src/entities/equipment-base.entity.ts',
  'nestjs-app/src/entities/character-stone.entity.ts',
  'nestjs-app/src/entities/character-skill.entity.ts',
  'nestjs-app/src/entities/character-residence.entity.ts',
  'nestjs-app/src/entities/character-mount.entity.ts',
  'nestjs-app/src/entities/character-medicine.entity.ts',
  'nestjs-app/src/entities/character-equipment-inventory.entity.ts',
  'nestjs-app/src/entities/character-detailed-attribute.entity.ts',
  'nestjs-app/src/entities/character-attribute.entity.ts',
  'nestjs-app/src/entities/character-attribute-points.entity.ts',
  'nestjs-app/src/entities/announcement.entity.ts',
  'nestjs-app/src/entities/gem-mining.entity.ts',
  'nestjs-app/src/entities/equipment-gem-socket.entity.ts',
  'nestjs-app/src/entities/character-religion.entity.ts',
  'nestjs-app/src/entities/character-equipment.entity.ts',
  'nestjs-app/src/entities/npc-cluster.entity.ts',
  'nestjs-app/src/entities/npc-detail.entity.ts',
  'nestjs-app/src/entities/basic-item.entity.ts',
  // Casino相关Entity文件
  'nestjs-app/src/modules/casino/entities/casino-dice-round.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-chibi-round.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-dice-bet.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-chibi-bet.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-rock-paper-scissors.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-ranking.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-config.entity.ts',
  'nestjs-app/src/modules/casino/entities/casino-operation-log.entity.ts'
];

function updateEntityFile (filePath) {
  try {
    if (!fs.existsSync(filePath)) {
      console.log(`⚠️  文件不存在: ${filePath}`);
      return false;
    }

    let content = fs.readFileSync(filePath, 'utf8');
    let modified = false;

    // 更新 @Column 映射
    const originalContent = content;

    // 将 name: "createdAt" 改为 name: "created_at"
    content = content.replace(/name:\s*"createdAt"/g, 'name: "created_at"');

    // 将 name: "updatedAt" 改为 name: "updated_at"
    content = content.replace(/name:\s*"updatedAt"/g, 'name: "updated_at"');

    if (content !== originalContent) {
      fs.writeFileSync(filePath, content, 'utf8');
      modified = true;
    }

    return modified;
  } catch (error) {
    console.error(`❌ 处理文件 ${filePath} 时出错:`, error.message);
    return false;
  }
}

function main () {
  console.log('🔄 开始更新Entity文件中的@Column映射...');
  console.log('');

  let successCount = 0;
  let totalCount = entityFiles.length;

  entityFiles.forEach((filePath, index) => {
    console.log(`[${index + 1}/${totalCount}] 处理: ${filePath}`);

    const modified = updateEntityFile(filePath);
    if (modified) {
      console.log(`✅ 已更新: ${filePath}`);
      successCount++;
    } else {
      console.log(`⏭️  无需更新: ${filePath}`);
    }
  });

  console.log('');
  console.log('📊 更新完成统计:');
  console.log(`   总文件数: ${totalCount}`);
  console.log(`   已更新: ${successCount}`);
  console.log(`   无需更新: ${totalCount - successCount}`);
  console.log('');
  console.log('✅ Entity文件更新完成！');
  console.log('');
  console.log('📝 接下来需要执行以下步骤：');
  console.log('1. 重启应用程序');
  console.log('2. 测试功能是否正常');
}

main();
